.
library(fma)
## Loading required package: forecast
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(fpp2)
## ── Attaching packages ────────────────────────────────────────────── fpp2 2.4 ──
## ✓ ggplot2 3.3.5 ✓ expsmooth 2.3
##
library(forecast)
library(readxl)
library(GGally)
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
##
## Attaching package: 'GGally'
## The following object is masked from 'package:fma':
##
## pigs
autoplot(gold)
autoplot(woolyrnq)
autoplot(gas)
b) What is the frequency of each series? Hint: apply the frequency()
function.
frequency(gold)
## [1] 1
frequency(woolyrnq)
## [1] 4
frequency(gas)
## [1] 12
A Frequency of gold is 1, which tells us that there do not exists seasonal patterns in the data.
A frequency of 4 in the woolyrnq dataset suggests seasonality that repeats every year with each quarter being an element in a given period
A frequency of gas is 12 tells us that seasonality occurs every year, with 12 elements in the period defining a single “season”
i <- which.max(gold)
i
## [1] 770
gold[i]
## [1] 593.7
Observation 770 was the outlier with a value of 593.7, this is confirmed by looking at the graph above
2a) You can read the data into R with the following script
tute1 <- read.csv('tute1.csv', header=T)
head(tute1, n = 10)
2b) Convert to Time Series
mytimeseries <- ts(tute1[,-1], start=1981, frequency=4)
autoplot(mytimeseries, facets=T) #facets=T seperates time series into seperate panels
ggAcf(tute1[-1])
From the diagonals it looks like we have a seasonality with a period of
4 and no trends in the data
3a)
retaildata <- read_excel("retail.xlsx", skip=1)
head(retaildata, n=6)
3b)
myts <- ts(retaildata[,"A3349873A"], frequency=12, start=c(1982,4)) #starts measurement in 1982 on time unit 4
head(myts, n=9)
## Apr May Jun Jul Aug Sep Oct Nov Dec
## 1982 62.4 63.1 59.6 61.9 60.7 61.2 62.1 68.3 104.0
3c)
autoplot(myts)
From the autoplot we can see both a trend and seasonality. Trends looks fairly linear. From this graph alone it is difficult to confirm the seasonality period.
ggAcf(myts)
The ACF confirms a trend due to slow convergence toward 0. Additionally
we can see that frequency should be 12 as there is seasonaility period
occuring every year.
ggseasonplot(myts, year.labels = T)
The seasonal plot shows us details on the seasonality. In this case we
can see a increases in March, with some exceptions, decreases in June,
jumps from October and Novemember that seem to have increased in 2007,
2012, 2013. Finally, from November to Decemeber we can see that 2007,
2012, and 2013 all have similar behavor that is distinct from the other
years.
gglagplot(myts)
ggsubseriesplot(myts)
Through the subseries plot we can see fairly weak seasonality in the first couple of months. This is because the means of each element are close to each other which suggests the effect of these months on the response is similar. Seasonality is shown more strongly in The latter half of the year.
help(goog)
autoplot(goog) +
labs(
x = 'Day',
y = "Closing Price",
title='GOOG Closing Stock Price'
)
4/5)
Writing Time Series
autoplot(writing)
ggsubseriesplot(writing)
ggseasonplot(writing, year.labels=T)
For Writing, we see a positive trend with strong seasonality. 1977 was a little unusual in that we expect a local max around March with slight differences between Janurary and Feburary. However, in 1977 we notice a peak in February. Furthermore, While we see a year over year increase, 1975 and 1976 did not follow the trend.
Fancy
autoplot(fancy)
ggsubseriesplot(fancy)
ggseasonplot(fancy, year.labels = T)
Overall the Fancy dataset shows a slight trend with increasing magnitudes. There is little noise occurring, which means the the seasonality remains fairly consistent. The only usual activity I observe is that December of 1990 remained similar to the 1989 in trend and magnitude unlike the others years which should increasing trend and magnitude.
a10
help(a10)
l <- labs(
title='Monthly Anti-diabetic Drug subsiby',
y = 'Monthly Gov Expenditure in Millions'
)
autoplot(a10) + l
ggsubseriesplot(a10) + l
ggseasonplot(a10, year.labels=T) + l
From the timeplot we see a trend, seasonality, with each season also having slight increasing magnitudes. 2007 was a little unusual in that the the rate of increase was larger. While 2008 is incomplete, we also notice a deviation from the trend in that there was no dip in April.
h02
help(h02)
l <- labs(
title = "Monthly Corticosteriod Subsidy in Australia (1991-2008)",
y = "Expenditure in Millions"
)
autoplot(h02) + l
ggsubseriesplot(h02) + l
ggseasonplot(h02, year.labels=T) + l
In the h02 dataset we have a slight trend intially that then starts to taper off over the years. 2008 was a little unusual in that April saw an increase while other years had a decrease during this month.
autoplot(hsales)
For Hsales we see both seasonality and cyclity.
ggAcf(hsales)
Because we see a fairly rapid decline in ACF, while there exists seasonality and cyclity, there does not exist a trend.
ggsubseriesplot(hsales)
The noisey subseries further supports cyclity
ggseasonplot(hsales, year.labels=T)
gglagplot(hsales)
US Deaths
autoplot(usdeaths)
We see little to no trend and seasonality. There does exist a little noise in that 1973 seems to be shiften up from the rest.
ggsubseriesplot(usdeaths)
subseries plot further confirms that 1973 was an outlier year in terms
of magnitude
ggseasonplot(usdeaths)
ggAcf(usdeaths)
gglagplot(usdeaths)
autoplot(bricksq)
We can observe trend and slight seasonality, this is best seen from the seasonal plot, which shows only sight changes across quarters
ggsubseriesplot(bricksq)
ggseasonplot(bricksq, year.labels = T)
ggAcf(bricksq)
From the ACF we can confirm trend with seasonality every year
gglagplot(bricksq)
autoplot(sunspotarea)
No trend with cycility. We do have increasing magnitudes up to a point. Based off of the ACF, the cycility period is about 10 years.
ggAcf(sunspotarea)
gglagplot(sunspotarea)
autoplot(gasoline)
the time plot itself reveals a trend and possible seasonality. Given the data it appears too noisey to determine the seasonality period.
ggseasonplot(gasoline, year.labels=T)
season plot is too noisey to clearly see seasonality, best to use ACF
ggAcf(gasoline)
ACF supports trend and seasonality occuring every year.
gglagplot(gasoline)
autoplot(arrivals, facets = T)
Seasonality between US and NZ seem similar. NZ, UK, and US follow similar trends. While Japan is similar, it has a starts to deviate from the trend of the other locations is the early 2000s.
ggpairs(as.data.frame(arrivals[,1:4]))
The scatterplot matrix confirms that US, UK, and NZ follow similar trends. With Japan having the weakest trend when compared to these 3 locations.
ggseasonplot(arrivals[,c('Japan')], year.labels = T)
ggseasonplot(arrivals[,c('US')], year.labels = T)
ggseasonplot(arrivals[,c('UK')], year.labels = T)
ggseasonplot(arrivals[,c('NZ')], year.labels = T)
In Japan, US, and UK we see dips in arrivals in between Q1 and Q2, NZ is different in that it actually sees an increase between Q1 and Q2. They all see increases between Q2 and Q3, particulary Japan, however, Japan and NZ see declines between Q3 and Q4.
ggsubseriesplot(arrivals[,c('Japan')])
ggsubseriesplot(arrivals[,c('US')])
ggsubseriesplot(arrivals[,c('UK')])
ggsubseriesplot(arrivals[,c('NZ')])
mypigs <- window(fma::pigs, start=1990)
autoplot(mypigs)
hard to tell if seasonality actually exists
ggAcf(mypigs)
Because multiple time-lags exceed the threshold, there likely exists temporal dependency. This violates the definition of white noise as it needs to be i.i.d
10
ddj <- diff(dj)
autoplot(ddj)
ggAcf(ddj)
The time plot suggests no trend. Unclear from the time plot alone if there exists seasonality. The ACF suggests the the difference is white noise. This is because 95% of the data lie below the threshold.